/*
 * JavaScript Library for dcloud
 * Copyright (c) 2015 mxchip.com
 * by Rocke 2015-08-11
 */
 (function(window) {
 	var m = {};

 	document.addEventListener("plusready", function(){
 		var B = window.plus.bridge, _MiCOBIND = 'micoBind', _MiCOMQTT = 'micoMqtt';
 		var micoPlugin = {
 			openmDNS : function (Argus1, successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOBIND, "startmDNS", [callbackID, Argus1]);
 			},
 			stopmDNS : function (successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOBIND, "stopmDNS", [callbackID]);
 			},
 			getSSId : function (successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOBIND, "getSSId", [callbackID]);
 			},
 			startEasyLink : function (Argus1,Argus2,successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOBIND, "startEasyLink", [callbackID,Argus1,Argus2]);
 			},
 			stopEasyLink : function (successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOBIND, "stopEasyLink", [callbackID]);
 			},
 			startMqtt : function (Argus1, Argus2, Argus3, Argus4, Argus5, successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOMQTT, "startMqtt", [callbackID, Argus1, Argus2, Argus3, Argus4, Argus5]);
 			},
 			recvMqttMsg : function (successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOMQTT, "recvMqttMsg", [callbackID]);
 			},
 			publishCommand : function (Argus1, Argus2,successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOMQTT, "publishCommand", [callbackID, Argus1, Argus2]);
 			},
 			stopRecvMqttMsg : function (successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOMQTT, "stopRecvMqttMsg", [callbackID]);
 			},
 			stopMqtt : function (successCallback, errorCallback ){
 				var success = typeof successCallback !== 'function' ? null : function(args){
 					successCallback(args);
 				},
 				fail = typeof errorCallback !== 'function' ? null : function(code){
 					errorCallback(code);
 				};
 				callbackID = B.callbackId(success, fail);
 				return B.exec(_MiCOMQTT, "stopMqtt", [callbackID]);
 			}					
 		};
 		window.plus.micoPlugin = micoPlugin;
 	}, true );

	//OpenmDNS
	m.startmDNS = function(serviceName, callback) {
		var sucm;
		var errm;
		plus.micoPlugin.openmDNS(serviceName,function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};
	//ClosemDNS
	m.stopmDNS = function(callback) {
		var sucm;
		var errm;
		plus.micoPlugin.stopmDNS(function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};
	//Get ssid
	m.getSSId = function(callback) {
		var sucm;
		var errm;
		plus.micoPlugin.getSSId(function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};
	//start easylink
	m.startEasyLink = function(wifissid, wifipsw, callback) {
		var sucm;
		var errm;
		plus.micoPlugin.startEasyLink(wifissid, wifipsw, function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};
	//stop Easylink
	m.stopEasyLink = function(callback) {
		var sucm;
		var errm;
		plus.micoPlugin.stopEasyLink(function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};
	//open mqtt server
	m.startMqtt = function(host,username,password,clientID,topic, callback) {
		var sucm;
		var errm;
		plus.micoPlugin.startMqtt(host,username,password,clientID,topic, function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};
	//start receive message
	m.recvMqttMsg = function(callback) {
		var sucm;
		var errm;
		plus.micoPlugin.recvMqttMsg(function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};
	//send command to mqtt service
	m.publishCommand = function(topic,command,callback) {
		var sucm;
		var errm;
		plus.micoPlugin.publishCommand(topic,command,function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};
	//stop reveice mqtt message
	m.stopRecvMqttMsg = function(callback) {
		var sucm;
		var errm;
		plus.micoPlugin.stopRecvMqttMsg(function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};
	//stop mqtt server
	m.stopMqtt = function(callback) {
		var sucm;
		var errm;
		plus.micoPlugin.stopMqtt(function(ret) {
			callback(JSON.parse(ret), errm);
		});
	};

	
	//获取设备列表
	m.getDevList = function(userToken, callback) {
		var sucm;
		var errm;
		AV.Cloud.run('getDevList', {
			userToken : userToken,
		}, {
			success : function(ret) {
				callback(ret.data, errm);
			},
			error : function(err) {
				//处理调用失败
				callback(sucm, err);
			}
		});
	};

	//获取owner权限的设备
	m.getAuthDev = function(userToken, callback) {
		var sucm;
		var errm;
		AV.Cloud.run('getAuthDev', {
			userToken : userToken,
		}, {
			success : function(ret) {
				callback(ret.data, errm);
			},
			error : function(err) {
				//处理调用失败
				callback(sucm, err);
			}
		});
	};

	//修改设备名称
	m.editDevName = function(appid, usertoken, devname, devid, callback) {
		var sucm;
		var errm;
		$.ajax({
			url : "http://api.easylink.io/v1/device/modify",
			type : 'POST',
			data : JSON.stringify({
				device_id : devid,
				alias : devname
			}),
			headers : {
				"Content-Type" : "application/json",
				"X-Application-Id" : appid,
				"Authorization" : "token " + usertoken
			},
			success : function(ret) {
				callback("success", errm);
			},
			error : function(err) {
				callback(sucm, err);
			}
		});
	};

	//删除设备
	m.deleteDev = function(appid, usertoken, devid, callback) {
		var sucm;
		var errm;
		$.ajax({
			url : "http://api.easylink.io/v1/device/delete",
			type : 'POST',
			data : JSON.stringify({
				device_id : devid
			}),
			headers : {
				"Content-Type" : "application/json",
				"X-Application-Id" : appid,
				"Authorization" : "token " + usertoken
			},
			success : function(ret) {
				callback("success", errm);
			},
			error : function(err) {
				callback(sucm, err);
			}
		});
	};

	//获取设备id
	m.getDevid = function(devip, devpsw, devtoken, callback) {
		var sucm;
		var errm;
		var ajaxurl = 'http://' + devip + ':8001/dev-activate';
		$.ajax({
			url : ajaxurl,
			type : 'POST',
			data : JSON.stringify({
				login_id : "admin",
				dev_passwd : devpsw,
				user_token : devtoken
			}),
			headers : {
				"Content-Type" : "application/json"
			},
			success : function(ret) {
				callback(ret, errm);
			},
			error : function(err) {
				callback(sucm, err);
			}
		});
	};

	//去云端绑定设备
	m.bindDevCloud = function(appid, usertoken, devtoken, callback) {
		var sucm;
		var errm;
		$.ajax({
			url : 'http://api.easylink.io/v1/key/authorize',
			type : 'POST',
			data : {
				active_token : devtoken
			},
			headers : {
				"Authorization" : "token " + usertoken,
				"X-Application-Id" : appid
			},
			success : function(ret) {
				callback(ret, errm);
			},
			error : function(err) {
				callback(sucm, err);
			}
		});
	};

	//授权设备
	m.authDev = function(appid, usertoken, phone, devid, role, callback) {
		var sucm;
		var errm;
		var action = role == "owner"? 2 : 1;
		$.ajax({
			url : 'http://api.easylink.io/v1/key/user/authorize',
			type : 'POST',
			data : {
				login_id : phone,
				owner_type : role,
				id : devid,
				action_type : action,
			},
			headers : {
				"Authorization" : "token " + usertoken,
				"X-Application-Id" : appid
			},
			success : function(ret) {
				callback(ret, errm);
			},
			error : function(err) {
				callback(sucm, err);
			}
		});
	};
	
	/*end*/
	window.$mico = m;

})(window);
